package org.darkforest.merchant.controller;

import org.darkforest.common.services.GiftCardTemplateService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.GiftCardTemplateMapper;
import org.darkforest.model.GiftCardTemplate;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by pc on 2017/4/6.
 */
@Controller
@RequestMapping(value="/giftcardtemplate")
public class GiftCardTemplateController extends WebController<GiftCardTemplateController> {
    @Autowired
    private GiftCardTemplateMapper giftCardTemplateMapper;
    @Autowired
    private GiftCardTemplateService giftCardTemplateService;

    @RequestMapping(value="/list")
    public ModelAndView list(GiftCardTemplate giftCardTemplate){
        ModelAndView view = new ModelAndView("giftcardtemplate/list");
        giftCardTemplate = giftCardTemplateService.selectByCondition(giftCardTemplate);
        view.addObject("page",giftCardTemplate);
        return view;
    }

    @RequestMapping(value="/toadd")
    public ModelAndView toadd(){
        ModelAndView view = new ModelAndView("giftcardtemplate/add");
        return view;
    }

    @RequestMapping(value="/doadd")
    public String doadd(HttpServletRequest request,GiftCardTemplate giftCardTemplate,MultipartHttpServletRequest multipartHttpServletRequest){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftCardTemplate.setCreaterId(sellerVo.getMemberId());
        giftCardTemplate.setCreateName(sellerVo.getMemberName());
        giftCardTemplate.setCreateTime(DateUtil.toTimeString(new Date()));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            giftCardTemplate.setCardImage(picture);
        }
        giftCardTemplateMapper.insert(giftCardTemplate);
        return "redirect:list";
    }

    @RequestMapping(value="/toedit")
    public ModelAndView toedit(Integer id){
        ModelAndView view = new ModelAndView("giftcardtemplate/edit");
        GiftCardTemplate giftCardTemplate = giftCardTemplateMapper.selectByPrimaryKey(id);
        view.addObject("giftCardTemplate",giftCardTemplate);
        return view;
    }

    @RequestMapping(value="/doedit")
    public String doedit(HttpServletRequest request,GiftCardTemplate giftCardTemplate,MultipartHttpServletRequest multipartHttpServletRequest){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftCardTemplate.setCreaterId(sellerVo.getMemberId());
        giftCardTemplate.setCreateName(sellerVo.getMemberName());
        giftCardTemplate.setCreateTime(DateUtil.toTimeString(new Date()));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            giftCardTemplate.setCardImage(picture);
        }
        giftCardTemplateMapper.updateByPrimaryKeySelective(giftCardTemplate);
        return "redirect:list";
    }
    @RequestMapping(value="/todel")
    public String todel(Integer id){
        return "list";
    }

}
